package com.ying.dogying.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.code.kaptcha.Producer;
import com.ying.dogying.service.*;
import com.ying.dogying.shiro.AccountProfileDto;
import com.ying.dogying.utils.RedisUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import javax.servlet.http.HttpServletRequest;

public class BaseController {

    @Autowired
    HttpServletRequest request;

    // 文章Service
    @Autowired
    ImPostService imPostService;

    // 类别Service
    @Autowired
    ImCategoryService imCategoryService;

    // 评论Service
    @Autowired
    ImCommentService imCommentService;

    // 用户Service
    @Autowired
    ImUserService imUserService;

    // 用户收藏文档Service
    @Autowired
    ImUserCollectionService imUserCollectionService;

    // 用户消息Service
    @Autowired
    ImUserMessageService imUserMessageService;

    // 聊天Service
    @Autowired
    ImChatService imChatService;

    // redis封装工具类
    @Autowired
    RedisUtils redisUtils;

    // 生产验证码图像和文本绘制
    @Autowired
    Producer producer;

    // esService
    @Autowired
    EsService esService;

    /**
     * 默认展示第一页，每页展示10条
     */
    public Page getPage() {
        int pageNumber = ServletRequestUtils.getIntParameter(request, "pageNumber", 1);
        int pageSize = ServletRequestUtils.getIntParameter(request, "pageSize", 10);
        return new Page(pageNumber, pageSize);
    }

    /**
     * 获取当前登录的信息
     */
    protected AccountProfileDto getProfile() {
        return (AccountProfileDto) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 获取当前登录的用户ID
     */
    protected Long getProfileId() {
        return getProfile().getId();
    }

}
